import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm";

export class CreateActivityFavorites1682513064000 implements MigrationInterface {
    public async up(queryRunner: QueryRunner): Promise<void> {
        // 检查表是否已存在
        const tableExists = await queryRunner.hasTable("activity_favorites");
        if (tableExists) {
            console.log('activity_favorites 表已存在，跳过创建');
            return;
        }

        await queryRunner.createTable(
            new Table({
                name: "activity_favorites",
                columns: [
                    {
                        name: "id",
                        type: "int",
                        isPrimary: true,
                        isGenerated: true,
                        generationStrategy: "increment",
                    },
                    {
                        name: "userId",
                        type: "int",
                    },
                    {
                        name: "activityId",
                        type: "int",
                    },
                    {
                        name: "createdAt",
                        type: "timestamp",
                        default: "CURRENT_TIMESTAMP",
                    },
                ],
            }),
            true
        );

        // 检查用户表名是否存在
        const userTableExists = await queryRunner.hasTable("users");
        const userTableName = userTableExists ? "users" : "user";

        // 检查活动表是否存在
        const activitiesTableExists = await queryRunner.hasTable("activities");
        if (!activitiesTableExists) {
            console.log('activities 表不存在，跳过创建外键');
            return;
        }

        // 修改外键约束中的表名
        await queryRunner.createForeignKey(
            "activity_favorites",
            new TableForeignKey({
                columnNames: ["userId"],
                referencedColumnNames: ["id"],
                referencedTableName: userTableName, // 使用检测到的表名
                onDelete: "CASCADE",
            })
        );

        await queryRunner.createForeignKey(
            "activity_favorites",
            new TableForeignKey({
                columnNames: ["activityId"],
                referencedColumnNames: ["id"],
                referencedTableName: "activities",
                onDelete: "CASCADE",
            })
        );
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        // 检查表是否存在
        const tableExists = await queryRunner.hasTable("activity_favorites");
        if (!tableExists) {
            console.log('activity_favorites 表不存在，跳过回滚');
            return;
        }

        const table = await queryRunner.getTable("activity_favorites");
        if (table) {
            const foreignKeys = table.foreignKeys;
            for (const foreignKey of foreignKeys) {
                await queryRunner.dropForeignKey("activity_favorites", foreignKey);
            }
        }
        await queryRunner.dropTable("activity_favorites");
    }
}




